(function(global) {
    // Global namespace
    var utils = utils || {};

    utils.ONE_DAY = 86400000;

    // Create new cookie
    utils.createCookie = function(name,value,days) {
      var expires, date;
      if (days) {
        date = new Date();
        date.setTime(date.getTime()+(days * utils.ONE_DAY));
        expires = "; expires="+date.toGMTString();
      } else {
        expires = "";
      }

      document.cookie = name+"="+value+expires+"; path=/";
    };

    // Read cookie
    utils.readCookie = function(name) {
      var i, c, ca, nameEQ = name + "=";
      ca = document.cookie.split(';');
      for(i=0;i < ca.length;i++) {
        c = ca[i];
        while (c.charAt(0)==' ') {
          c = c.substring(1,c.length);
          if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length,c.length);
          }
        }
      }
      return null;
    };

    // delete cookie
    utils.eraseCookie = function(name) {
      utils.createCookie(name,"",-1);
    };

    // Write latitude and longitude into submit form
    utils.writeLatLng = function(latitude, longitude) {
      latField = document.getElementById('id_latitude');
      lngField = document.getElementById('id_longitude');

      if(latField && lngField) {
        latField.value = latitude.toString();
        lngField.value = longitude.toString();
      }

    };                          

    utils.writePositionInfo = function(data) {
    //  latField = document.getElementById('id_latitude');
     // lngField = document.getElementById('id_longitude');
      countryField = document.getElementById('id_country');
      regionField = document.getElementById('id_region');
      cityField = document.getElementById('id_city');
      districtField = document.getElementById('id_district');

      /*
      if(latField && lngField) {
        latField.value = latitude.toString();
        lngField.value = longitude.toString();
      }*/

      if (data.country) {
        countryField.value = country.toString();
      }

      if (data.region) {
        regionField.value = region;
      }

      if (data.city) {
        cityField.value = city.toString();
      }

      if (data.district) {
        districtField.value = district.toString();
      }
    };

    // Clear all members of an arrary, useful for makers
    utils.clearArray = function(array) {
      var i;
      if (array.length > 0) {
        for (i = 0; i < array.length; i++) {
          array.pop();
        }
      }
    };

    utils.createInfoWindowHTML = function(data) {
      var html = '',
          uid = data.uid || 'test',
          title = data.name,
          address = data.address,
          //image, thumb,
          image = data.image,
          thumb = data.thumb,
          description = data.description;


      if (image && thumb) {
        html = '<a href="/place/' + uid + '"><img class="infowindow-thumb" src="/place/thumb/' + thumb + '" />';
      } else {
        html = '<a href="/place/' + uid + '"><img class="infowindow-thumb" src="/static/img/place/default-thumb.png" /><a href="/place/' + uid + '"></a>';
      }


      console.log(html);
      return '<div class="infowindow-html">' + html + '</div>';

    };

    utils.createResultsHTML = function(results) {
      var html = '<h3>搜索结果</h3><hr /><div id="map-search"></div>', 
          i, name, link, description;

      //console.log(results);
      //////////////////////////////////////////////////////
      //  Store all the search results in the browser ?  //
      /////////////////////////////////////////////////////
      if (results.prev) {
        html += '<button type="button" id="search-prev" class="search-index" name="' + results.prev + '"> < 前一页 </button>';
      }

      if (results.next) {
        html += '<button type="button" id="search-next" class="search-index" name="' + results.next + '"> 后一页 > </button>';
      }

      html += '<ol id="results">';

      for (i = 0; i < results.places.length; i++) { // forEach?
        name = results.places[i].name;
        link = '/place/' + results.places[i].uid;
        description = results.places[i].description.substr(0, 100);
        html += '<li class="result" id="result' + i +'" ><p class="result-name">' + name + '</p><p class="results-description">' + description + '</p><a href="' + link + '">去看看</a>';
      }

      console.log(html);
      return '<div id="search-results">' + html + '</ol></div>';
    };

    utils.createSideBarHTML = function(ads) {
      // Create sidebar content
      // Mostly will be advotise
      var html = '<h4>付费广告</h4>';

      return '<div id="ads">' + html + '</div>';
    };

    utils.sameOrigin = function(url) {
      // url could be relative or scheme relative or absolute
      var host = document.location.host; // host + port
      var protocol = document.location.protocol;
      var sr_origin = '//' + host;
      var origin = protocol + sr_origin;
      // Allow absolute or scheme relative URLs to same origin
      return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
      (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
      // or any other URL that isn't scheme relative or absolute i.e relative.
      !(/^(\/\/|http:|https:).*/.test(url));
  };

  utils.safeMethod = function(method) {
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
};

global.utils = utils;

})(window);
